#ifndef _CAPP_H_
    #define _CAPP_H_

#include "CApp.h"
#include "CAppStateManager.h"
#include <SDL/SDL_ttf.h>
#include <SDL/SDL_net.h>

int main(int argc, char* argv[]) {
	CApp theApp;
	return theApp.onExecute();
}

CApp::CApp(){
	surfDisplay = NULL;
	running = true;
}

int CApp::onExecute(){
		if(onInit() == false){
			return -1;
		}

		SDL_Event event;

		while(running){
			while(SDL_PollEvent(&event)){
				onEvent(&event);
			}
			onLoop();
			onRender();
		}

		onCleanUp();
		return 0;
	}

bool CApp::onInit() {
	if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
		std::cout << "SDL_Init: " << SDL_GetError() << std::endl;
		return false;
	}

	if ((surfDisplay = SDL_SetVideoMode(1024, 768, 32,
			SDL_HWSURFACE | SDL_DOUBLEBUF)) == NULL) {
		std::cout << "Create Display: " << std::endl;
		return false;
	}

	if(TTF_Init() < 0){
		std::cout << "Iniciar a TTF " << std::endl;
	}

	if(SDLNet_Init() < 0){
		std::cout << "SDLNet_Init: " << SDLNet_GetError() << std::endl;
		return false;
	}

	//SDL_EnableKeyRepeat(1, SDL_DEFAULT_REPEAT_INTERVAL / 3);

	CAppStateManager::setActiveAppState(APPSTATE_INTRO);

	return true;
}

void CApp::onEvent(SDL_Event* event){
	CEvent::onEvent(event);
	CAppStateManager::onEvent(event);

}

void CApp::onExit(){
	running = false;
	SDLNet_Quit();
}

void CApp::onLoop() {
    CAppStateManager::onLoop();

  //CFPS::FPSControl.OnLoop();

    //char Buffer[255];
    //sprintf(Buffer, "FPS: %d", CFPS::FPSControl.GetFPS());
    //SDL_WM_SetCaption(Buffer, Buffer);
}

void CApp::onRender(){
	CAppStateManager::onRender(surfDisplay);
	SDL_Flip(surfDisplay);
}

void CApp::onCleanUp(){
    CAppStateManager::setActiveAppState(APPSTATE_NONE);
    TTF_Quit();
    SDL_FreeSurface(surfDisplay);
    SDL_Quit();
}

#endif
